package com.mairuide._frame.utils.sheethandler.oneKey;

import com.mairuide.one_click_search.pojo.DataSource;
import com.mairuide.one_click_search.pojo.DataTemplate;
import com.mairuide.one_click_search.pojo.DataTemplateField;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.eventusermodel.ReadOnlySharedStringsTable;
import org.apache.poi.xssf.eventusermodel.XSSFReader;
import org.apache.poi.xssf.eventusermodel.XSSFSheetXMLHandler;
import org.apache.poi.xssf.model.StylesTable;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description
 * @auther 刘中华
 * @create 2019-04-19 18:21
 */
public class OneKeyPOIFactory {

    public static Map<String,Object> deal(InputStream inputStream,Map<String , Object> paramMap) throws Exception{
        Map<String,Object> map = new HashMap<>();
        long a= System.currentTimeMillis();
        if (StringUtils.endsWith(paramMap.get("excelName").toString(),"xls")){
            POIFSFileSystem pfs = new POIFSFileSystem(inputStream); // 解析2003版本
            Workbook wb = new HSSFWorkbook(pfs,true);
            OneKeyExcel03SheetHandler oneKeyExcel03SheetHandler = new OneKeyExcel03SheetHandler();
            oneKeyExcel03SheetHandler.setDataTemplateFieldMap((Map<String, DataTemplateField>)paramMap.get("dataTemplateFieldMap"));
            oneKeyExcel03SheetHandler.setDataTemplate((DataTemplate)paramMap.get("dataTemplate"));
            oneKeyExcel03SheetHandler.setParaMap(paramMap);
            oneKeyExcel03SheetHandler.deal(wb);
            List<DataSource> data = oneKeyExcel03SheetHandler.getData();
            if (data.size()>0){
                oneKeyExcel03SheetHandler.doSomething();
                oneKeyExcel03SheetHandler.setData(new ArrayList<DataSource>());
            }
            map=oneKeyExcel03SheetHandler.getResultMap();
        }else{
            //1.根据excel报表获取OPCPackage
            OPCPackage opcPackage = OPCPackage.open(inputStream);
            //2.创建XSSFReader
            XSSFReader reader = new XSSFReader(opcPackage);
            //3.获取SharedStringTable对象
            ReadOnlySharedStringsTable readOnlySharedStringsTable = new ReadOnlySharedStringsTable(opcPackage);
            //4.获取styleTable对象
            StylesTable stylesTable = reader.getStylesTable();
            //5.创建Sax的xmlReader对象
            XMLReader xmlReader = XMLReaderFactory.createXMLReader();
            //6.注册事件处理器
            OneKeySheetHandler sheetHandler = new OneKeySheetHandler();
            sheetHandler.setDataTemplateFieldMap((Map<String, DataTemplateField>)paramMap.get("dataTemplateFieldMap"));
            sheetHandler.setDataTemplate((DataTemplate)paramMap.get("dataTemplate"));
            sheetHandler.setParaMap(paramMap);
            XSSFSheetXMLHandler xmlHandler = new XSSFSheetXMLHandler(stylesTable,readOnlySharedStringsTable, sheetHandler,false);
            xmlReader.setContentHandler(xmlHandler);
            //7.逐行读取
            XSSFReader.SheetIterator sheetIterator = (XSSFReader.SheetIterator) reader.getSheetsData();
            while (sheetIterator.hasNext()) {
                InputStream stream = sheetIterator.next();
                InputSource is = new InputSource(stream);
                xmlReader.parse(is);
            }
            //System.out.println("一共耗时："+(System.currentTimeMillis()-a)/1000);
            List<DataSource> data = sheetHandler.getData();
            if (data.size()>0){
                sheetHandler.doSomething();
            }
            map=sheetHandler.getResultMap();
        }
        System.out.println("一共耗时："+(System.currentTimeMillis()-a)/1000);
        return map;
    }
}
